package com.aat.high.orm.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.aat.high.orm.bean.User;

@Repository
public class UserDaoSJImpl implements UserDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	private String queryBean = "SELECT id,account,passwd,salt,roles," + "auths,locked,flag,token,nickname,"
			+ "ts,num01,str01,str02,str03 " + "FROM base_user WHERE id = ? ";

	private String queryAll = "SELECT id,account,passwd,salt,roles," + "auths,locked,flag,token,nickname,"
			+ "ts,num01,str01,str02,str03 " + "FROM base_user ";

	private RowMapper<User> rowMapper = new RowMapper<User>() {

		@Override
		public User mapRow(ResultSet rs, int rowNum) throws SQLException {
			User u = new User();
			u.setId(rs.getString("id"));
			u.setAccount(rs.getString("account"));
			u.setPasswd(rs.getString("passwd"));
			u.setSalt(rs.getString("salt"));
			u.setRoles(rs.getString("roles"));

			u.setAuths(rs.getString("auths"));
			u.setLocked(rs.getString("locked"));
			u.setFlag(rs.getString("flag"));
			u.setToken(rs.getString("token"));
			u.setNickname(rs.getString("nickname"));

			u.setTs(rs.getDate("ts"));
			u.setNum01(rs.getInt("num01"));
			u.setStr01(rs.getString("str01"));
			u.setStr02(rs.getString("str02"));
			u.setStr03(rs.getString("str03"));

			return u;
		}
	};

	@Override
	public User queryBean(String id) {
		return jdbcTemplate.queryForObject(queryBean, rowMapper, id);
	}

	@Override
	public List<User> queryAll() {
		return jdbcTemplate.query(queryAll, rowMapper);
	}

}
